package com.wmlive.hhvideo.heihei.personal.presenter;

import android.text.TextUtils;

import cn.wmlive.hhvideo.R;
import com.wmlive.hhvideo.common.base.BasePresenter;
import com.wmlive.hhvideo.common.network.HttpConstant;
import com.wmlive.hhvideo.heihei.beans.personal.ListFollowerFansResponse;
import com.wmlive.hhvideo.heihei.beans.splash.InitCatchData;
import com.wmlive.hhvideo.heihei.personal.view.IFocusView;
import com.wmlive.networklib.entity.BaseResponse;
import com.wmlive.networklib.entity.DCNetObserver;

/**
 * Created by XueFei on 2017/6/1.
 *
 * 关注
 */

public class FocusPresenter extends BasePresenter<IFocusView> {
    public FocusPresenter(IFocusView view) {
        super(view);
    }

    /**
     * 获取关注列表
     * @param user_id
     * @param offset
     */
    public void getFocusList(long user_id, int offset) {
        String url = InitCatchData.userListFollower();
        if (!TextUtils.isEmpty(url)) {
            executeRequest(HttpConstant.TYPE_PERSONAL_FOCUS_LIST, getHttpApi().getListFollower(url, user_id, offset))
                    .subscribe(new DCNetObserver<ListFollowerFansResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, ListFollowerFansResponse response) {
                            if (null != viewCallback) {
                                viewCallback.handleFocusSucceed(response);
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            if (null != viewCallback) {
                                viewCallback.handleFocusFailure(message);
                            }
                        }
                    });
        } else if (null != viewCallback) {
            viewCallback.handleNetError();
        }
    }

    /**
     * 是否关注
     * @param isFollow
     * @param user_id
     */
    public void updateFocusState(final boolean isFollow, long user_id) {
        String url = "";
        int requstCode = 0;
        if (isFollow) {
            url = InitCatchData.userUnFollowUsr();
            requstCode = HttpConstant.TYPE_PERSONAL_FOCUS_UNFOLLOW;
        } else {
            url = InitCatchData.userFollowUser();
            requstCode = HttpConstant.TYPE_PERSONAL_FOCUS_FOLLOW;
        }

        if (!TextUtils.isEmpty(url)) {
            executeRequest(requstCode, getHttpApi().updateFollowerState(url, user_id))
                    .subscribe(new DCNetObserver<BaseResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, BaseResponse response) {
                            if (null != viewCallback) {
                                if(!isFollow){
                                    viewCallback.handleFocusClickSucceed(true,R.string.user_follower);
                                }else{
                                    viewCallback.handleFocusClickSucceed(false,R.string.user_unfollower);
                                }
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            if (null != viewCallback) {
                                viewCallback.handleFocusClickFailure(message);
                            }
                        }
                    });
        } else if (null != viewCallback) {
            viewCallback.handleNetError();
        }
    }
}
